# !/usr/bin/python
# -*- coding: utf-8-*-
# Filename: Socket_Server.py

import socket
import threading

import H_socket

# IP
SERVER_IP = '192.168.1.55'
# 端口
LISTENPORT = 5010
# 最大连接数
CONNECT_MAXNUMBER = 2
# 超时
CONNECT_TIMEOUT = 2
# 最大读取字节数
RECV_MAXSIZE = 1024


# socket作成
Address = ( SERVER_IP, LISTENPORT )
# 设置socket属性 （通信类型，套接字类型）
ServerSocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
# 与IP和端口绑定
ServerSocket.bind( Address )
# 开始监听
ServerSocket.listen( CONNECT_MAXNUMBER )

# Log文件打开
ServerLogFile = open( 'S_Log.log', 'a' )

# 等待客户端连接循环
while True :
	try :
		print( 'Client Waiting ...' )
		# 等待连接
		ConnectSocket, ClientAddr = ServerSocket.accept()

		# 连接对象IP地址
		print( ClientAddr );

		# 读取线程建立
		RecvThread = threading.Thread( target = H_socket.Socket_Recv, args = ( ConnectSocket, ClientAddr, ServerLogFile ) )
		# 送信线程建立
		SendThread = threading.Thread( target = H_socket.Socket_Send, args = ( ConnectSocket, ClientAddr, ServerLogFile ) )
		# 读取线程启动
		RecvThread.start()
		# 送信线程启动
		SendThread.start()

		# 等待线程结束
		# RecvThread.join()
		SendThread.join()

		# socket关闭
		print( "socket close!" )
		ConnectSocket.close()

	except KeyboardInterrupt :
		# ctrl + c
		print( 'you have Ctrl+C, Now Quit!' )
		# socket关闭
		ConnectSocket.close()
		# Log文件关闭
		ServerLogFile.close()

input()

